package org.alex.samples.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.alex.samples.model.ExamScore;
import org.alex.samples.model.Subject;
import org.alex.samples.service.IExamScoreService;
import org.alex.samples.service.IExamUserSubjectService;
import org.alex.samples.service.ISubjectService;
import org.alex.samples.service.IUserService;
import org.alex.samples.vo.CommonMessageResultVO;
import org.alex.samples.vo.SubjectVO;
import org.alex.samples.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class SubjectController {
	
	@Autowired 
	ISubjectService SubjectService;
	
	@Autowired 
	IUserService userService;
	
	@Autowired
	IExamScoreService examScoreService;
	
	@Autowired
	IExamUserSubjectService examUserSubjectService;
	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping("/subject")
	public String subjectList(Model model){
		List<Subject> subjectList = SubjectService.FindAllSubject();
		model.addAttribute("edit", messageSource.getMessage("page.index.edit", null,LocaleContextHolder.getLocale()));
		model.addAttribute("subjectList", subjectList);
		return "subject/subjectList";
	}
	
	@RequestMapping("/subjects")
	public String tostudentSubjectList(Model model,HttpSession session){
		UserVO uservo = (UserVO) session.getAttribute("user");
		List<SubjectVO> studentSubjectList = examUserSubjectService.FindAllSubjectByU(uservo.getId());
		model.addAttribute("studentSubjectList", studentSubjectList);
		return "subject/studentSubjectList";
	}
	
	@RequestMapping(value = "getSubjectScore")
	@ResponseBody
	public SubjectVO gerSubjectScoreList(Integer subjectId,HttpSession session){
		SubjectVO subjectvo = new SubjectVO();
		UserVO uservo = (UserVO) session.getAttribute("user");
		ExamScore examScore = examScoreService.getScoreByUIdSId(uservo.getId(), subjectId);
		subjectvo.setTestscore(examScore.getScore());
		subjectvo.setUsername(uservo.getName());
		Date time = examScore.getExamTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		subjectvo.setExamTime(sdf.format(time));
		subjectvo.setName(SubjectService.FindSubjectById(subjectId).getName());
		return subjectvo;
	}
	
	@RequestMapping(value = "saveSubject")
	@ResponseBody
	public CommonMessageResultVO saveSubject(SubjectVO subjectvo,Model model){
		CommonMessageResultVO resultvo = new CommonMessageResultVO();
		resultvo.setSuccess(false);
		try{
			Subject subject = new Subject();
			BeanUtils.copyProperties(subjectvo, subject);
			SubjectService.createSubject(subject);
			resultvo.setSuccess(true);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultvo;
	}
	
	@RequestMapping(value = "getSubjectById")
	@ResponseBody
	public SubjectVO getSubjectById(int subjectId,Model model){
		SubjectVO subjectvo = new SubjectVO();
		subjectvo = SubjectService.FindSubjectById(subjectId);
		return subjectvo;
	}
	
	@RequestMapping(value = "saveEditSubject")
	@ResponseBody
	public CommonMessageResultVO saveEditSubject(SubjectVO subjectvo,Model model){
		CommonMessageResultVO resultvo = new CommonMessageResultVO();
		resultvo.setSuccess(false);
		if(subjectvo != null){
			try{
				SubjectService.editSubject(subjectvo);
				resultvo.setSuccess(true);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		return resultvo;
	}
	
	@RequestMapping(value = "subjectStatusEdit")
	@ResponseBody
	public CommonMessageResultVO subjectStatusEdit(SubjectVO subjectvo,Model model){
		CommonMessageResultVO resultvo = new CommonMessageResultVO();
		resultvo.setSuccess(false);
		try{
			SubjectVO tagerSubject = SubjectService.FindSubjectById(subjectvo.getId());
			tagerSubject.setStatus(subjectvo.isStatus());
			SubjectService.editSubject(tagerSubject);
			resultvo.setSuccess(true);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultvo;
	}
	
	@RequestMapping(value = "getAllStudent")
	@ResponseBody
	public Map<String,Object> getAllStudent(Integer id,Model model) throws Exception{
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("allUser", userService.findAllUsers());
		result.put("selectUser", SubjectService.findAllUserBySubject(id));
		return result;
	}
	
	@RequestMapping(value = "saveSubjectStudents")
	@ResponseBody
	public CommonMessageResultVO saveSubjectStudents(@RequestParam(name="subjectStudents[]")Integer[] subjectStudent,Integer subjectId){
		CommonMessageResultVO resultvo = new CommonMessageResultVO();
		resultvo.setSuccess(false);
		List<Integer> list = new ArrayList<>();
		for(Integer s:subjectStudent){
			list.add(s);
		}
		try{
			SubjectService.saveSubjectStudent(list,subjectId);
			resultvo.setSuccess(true);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultvo;
	}
	
	@RequestMapping(value = "deleteSubject")
	@ResponseBody
	public CommonMessageResultVO deleteSubject(Integer subjectId,Model model){
		CommonMessageResultVO resultvo = new CommonMessageResultVO();
		resultvo.setSuccess(false);
		try{
			SubjectVO s = SubjectService.FindSubjectById(subjectId);
			if(s != null){
				SubjectService.deleteSubject(subjectId);
				resultvo.setSuccess(true);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultvo;
	}
	
	
}
